package com.dudu.controller;


import com.alibaba.fastjson.JSONObject;
import com.dudu.domain.Building;
import com.dudu.domain.Room;
import com.dudu.domain.Roomtype;
import com.dudu.service.BuildingService;
import com.dudu.service.RoomService;
import com.dudu.service.RoomTypeService;
import com.dudu.tools.ServletUtil;
import com.dudu.tools.StringUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.net.Inet4Address;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 教程页面
 * Created by tengj on 2017/3/13.
 */
@Controller
@RequestMapping("/room")
public class RoomController {
    @Autowired
    private RoomService roomService;
	@Autowired
	RoomTypeService roomTypeService;
	@Autowired
	BuildingService buildingService;
    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }	

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, List<Room>> roomList(){

    	List<Room> roomList=roomService.getAllRoom();

        Map<String, List<Room>> map=new HashMap<>();
        map.put("roomlist", roomList);
        return map ;
    }
    /**
     * 添加room页面
     * @return add.html roomTypeidlist
     */
    @RequestMapping("/toadd")
    @ResponseBody    
    public ModelAndView addRoom(){

        List<Roomtype> roomtypesList=roomTypeService.getAllRoomType();
        List<Building> buildingList=buildingService.getAllBuilding();        
        ModelAndView modelAndView = new ModelAndView("/addRoom");
        
        modelAndView.addObject("roomtypesList", roomtypesList);
        modelAndView.addObject("buildingList", buildingList);
        return modelAndView;
    }

    /**
     * 保存room到数据库
     * @return int 
     */
    @RequestMapping("/save")
    @ResponseBody    
    public Map<String , List<Room>> saveRoom(Room room){
    	int resut=roomService.addRoom(room);
    	System.out.println(resut);
    	return this.roomList();
    } 
    
    @RequestMapping("/delete")
    @ResponseBody    
    public Map<String, List<Room>> deleteRoom(Integer id ){
    	Room room=new Room();
    	room.setId(id);
    	int result=roomService.deletRoom(room);
    	return this.roomList();
    } 
    
    @RequestMapping("/toupdate")
    @ResponseBody    
    public ModelAndView toupdate(Integer id){
    	Room room=new Room();
        room=roomService.selectRoomById(id);
        System.out.println(room.getName());

        ModelAndView modelAndView = new ModelAndView("text");
        modelAndView.addObject("room", room);
        List<Roomtype> roomtypesList=roomTypeService.getAllRoomType();
        List<Building> buildingList=buildingService.getAllBuilding();               
        modelAndView.addObject("roomtypesList", roomtypesList);
        modelAndView.addObject("buildingList", buildingList);
        return modelAndView;
    } 
    

    
    /**
     * 保存room到数据库
     * @return int 
     */
    @RequestMapping("/update")
    @ResponseBody    
    public Map<String, List<Room>> updateRoom(Room room){
    	int resut=roomService.updateRoom(room);
    	System.out.println(resut);
    	return this.roomList();
    }
    @RequestMapping("/test")
    @ResponseBody    
    public Map<String, List<Room>> test(int roomtypeid){

    	System.out.println(roomtypeid);

    	return this.roomList();
    } 
    
  
}